package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductVo;
import cn.itcast.vo.ExportResult;
import cn.itcast.vo.ExportVo;
import com.alibaba.dubbo.config.annotation.Method;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController {

    @Reference
    private ContractService contractService;
    @Reference
    private ExportService exportService;
    @Reference
    private ExportProductService exportProductService;


    @RequestMapping(value = "/contractList",name = "展示已提交的合同列表数据")
    public String findAll(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize){

//        查询购销合同 应该有什么条件  companyId   按照生成时间排序 降序
        ContractExample example = new ContractExample();
        ContractExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getCompanyId()).andStateEqualTo(1);
        example.setOrderByClause("create_time desc");
        PageInfo<Contract> pageInfo = contractService.findAll(example,page,pageSize);
        request.setAttribute("page",pageInfo); //这个 “page” 不能写错，因为页面就是取的是page中的值
        return "cargo/export/export-contractList";
    }

    @RequestMapping(value = "/list",name = "展示报运单列表数据")
    public String list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){

//        查询报运 应该有什么条件  companyId   按照生成时间排序 降序
        ExportExample example = new ExportExample();
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getCompanyId());
        example.setOrderByClause("create_time desc");
        PageInfo<Export> pageInfo = exportService.findAll(example,page,pageSize);
        request.setAttribute("page",pageInfo); //这个 “page” 不能写错，因为页面就是取的是page中的值
        return "cargo/export/export-list";
    }


    @RequestMapping(value = "/toExport",name = "进入到填写报运单的页面")
    public String toExport(String id){
        request.setAttribute("id",id);
        return "cargo/export/export-toExport";
    }
    @RequestMapping(value = "/edit",name = "进入到保存报运单的方法")
    public String edit(Export export){

        if(StringUtils.isEmpty(export.getId())){ //表示新增方法
            export.setId(UUID.randomUUID().toString());
            export.setCompanyId(getCompanyId());
            export.setCompanyName(getCompanyName());
            export.setInputDate(new Date());
            export.setCreateBy(getUser().getId());
            export.setCreateDept(getUser().getDeptId());
            export.setState(0);
            exportService.save(export);
        }else{
            exportService.update(export);
        }
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "进入到修改报运单的页面")
    public String toUpdate(String id){
//        报运单对象和这个报运单下的货物集合
        Export export = exportService.findById(id);
        request.setAttribute("export",export);

        List<ExportProduct> exportProductList  = exportProductService.findByExportId(id);
        request.setAttribute("eps",exportProductList);

        return "cargo/export/export-update";
    }


    @RequestMapping(value = "/submit",name = "提交报运单的方法")
    public String submit(String id){
        Export export = new Export();
        export.setId(id);
        export.setState(1);
        exportService.update(export);
        return "redirect:/cargo/export/list.do";
    }


    @RequestMapping(value = "/exportE",name = "电子报运方法")
    public String exportE(String id){//报运单id
        Export export = exportService.findById(id);
        ExportVo exportVo = new ExportVo();//exportVo中包含的是报运单数据和报运单下的货物数据
        BeanUtils.copyProperties(export,exportVo);
        exportVo.setExportId(id);


        List<ExportProduct> exportProducts = exportProductService.findByExportId(id);

        for (ExportProduct exportProduct : exportProducts) {
            ExportProductVo exportProductVo =new ExportProductVo();
            BeanUtils.copyProperties(exportProduct,exportProductVo);
            exportProductVo.setEid(id);
            exportProductVo.setExportProductId(exportProduct.getId());
            exportVo.getProducts().add(exportProductVo);
        }

        WebClient.create("http://localhost:8182/ws/export/ep/").post(exportVo);


//        调用查询结果方法
        ExportResult exportResult = WebClient.create("http://localhost:8182/ws/export/ep/" + id).get(ExportResult.class);
        exportService.updateExport(exportResult);

        return "redirect:/cargo/export/list.do";
    }


}

